package com.beyondsoft.mcms.common;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;

import org.springframework.security.authentication.encoding.MessageDigestPasswordEncoder;
import org.springframework.security.core.context.SecurityContextHolder;

import com.beyondsoft.mcms.domain.Conference;
import com.beyondsoft.mcms.domain.MemberInfo;
import com.beyondsoft.mcms.domain.Users;
import com.beyondsoft.mcms.reference.AuthRole;

public class CommonUtils {

	public static AuthRole getCurrentRole() {
		String username = getCurrentUsername();
		Users users = Users.findUsersesByUsernameEquals(username).getSingleResult();
		return users.getAuthority();
	}

	public static Users getCurrentUser() {
		String username = getCurrentUsername();
		return Users.findUsersesByUsernameEquals(username).getSingleResult();
	}

	public static String getCurrentUsername() {
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		return username;
	}

	public static MemberInfo getCurrentMember() {
		String username = getCurrentUsername();
		MemberInfo memberInfo = MemberInfo.findMemberInfoesByUsernameEquals(username).getSingleResult();
		return memberInfo;
	}

	public static Boolean usernameExisted(String username) {
		TypedQuery<Users> q = Users.findUsersesByUsernameEquals(username);
		if (q != null && q.getResultList().size() > 0) {
			return true;
		}
		return false;
	}

	public static Boolean memberNameExisted(String memberName) {
		TypedQuery<MemberInfo> q = MemberInfo.findMemberInfoesByMemberNameEquals(memberName);
		if (q != null && q.getResultList().size() > 0) {
			return true;
		}
		return false;
	}

	public static List<AuthRole> getEnumAuthRole() {
		List<AuthRole> authroles = new ArrayList<AuthRole>();
		if (!AuthRole.ADMIN.equals(getCurrentRole()))
			authroles.add(AuthRole.ADMIN);
		authroles.add(AuthRole.ORDINARY);
		authroles.add(AuthRole.PREUSER);
		return authroles;
	}

	public static String encodingPWD(String rawPwd) {
		MessageDigestPasswordEncoder md5 = new MessageDigestPasswordEncoder("MD5");
		md5.setEncodeHashAsBase64(false);
		return md5.encodePassword(rawPwd, null);
	}

	public static List<Conference> convertToNoRepeatedList(List<Conference> objects) {

		List<Conference> selects = new ArrayList<Conference>();
		for (int i = 0; i < objects.size(); i++) {
			Conference obj = objects.get(i);
			for (int j = i + 1; j < objects.size(); j++) {
				Conference reobj = objects.get(j);
				if (obj.equals(reobj)) {
					objects.remove(reobj);
					j--;
				}
			}

			selects.add(obj);
		}
		return selects;

	}

	public static String getRequestAddr(HttpServletRequest request) {
		String requestURL = request.getRequestURL().toString();
		String[] s = requestURL.split("/", 4);
		return s[0].trim() + "//" + s[2].trim();
	}
	
	public static void main(String[] args){
		System.out.println(encodingPWD("123"));
	}
}
